<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Swing文档大全 - Swing中树(Tree)的使用与介绍 - 虾米博客 - ITeye技术网站</title>
    <meta name="description" content="10-1:使用JTree组件：&nbsp;&nbsp; java.lang.Object&nbsp;&nbsp;&nbsp;&nbsp; --java.awt.Component&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --java.awt.Container&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --javax.swing.JComponent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --javax.swing.JTreeJTree构造函数:JTree():建立一棵系统默认的树。JTree(Hashtable value):利用Hash ...">
    <meta name="keywords" content="Swing, F#, 网络应用, XP, 项目管理 Swing文档大全 - Swing中树(Tree)的使用与介绍">
    <link rel="shortcut icon" href="http://tntxia.iteye.com/images/favicon.ico" type="image/x-icon">
    <link rel="search" type="application/opensearchdescription+xml" href="http://tntxia.iteye.com/open_search.xml" title="ITeye">
    <link href="http://tntxia.iteye.com/rss" rel="alternate" title="虾米博客" type="application/rss+xml">
    <link href="682834_files/blog.css" media="screen" rel="stylesheet" type="text/css">
<link href="682834_files/blue.css" media="screen" rel="stylesheet" type="text/css">
    <script src="682834_files/ga.js" async="" type="text/javascript"></script><script src="682834_files/ga.js" async="" type="text/javascript"></script><script src="682834_files/application.js" type="text/javascript"></script>
    <script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-535605-1']);
  _gaq.push(['_setDomainName', 'iteye.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


      <link href="682834_files/SyntaxHighlighter.css" media="screen" rel="stylesheet" type="text/css">
  <script src="682834_files/shCoreCommon.js" type="text/javascript"></script>
<script src="682834_files/hotkey.js" type="text/javascript"></script>
  <script src="682834_files/code_favorites.js" type="text/javascript"></script>
<script src="682834_files/weiboshare.js" type="text/javascript"></script>
  </head>
  <body>
    <div id="header">
      <div id="blog_site_nav">
  <a href="http://www.iteye.com/" class="homepage">首页</a>
  <a href="http://www.iteye.com/news">资讯</a>
  <a href="http://www.iteye.com/magazines">精华</a>
  <a href="http://www.iteye.com/forums">论坛</a>
  <a href="http://www.iteye.com/ask">问答</a>
  <a href="http://www.iteye.com/blogs">博客</a>
  <a href="http://www.iteye.com/blogs/subjects">专栏</a>
  <a href="http://www.iteye.com/groups">群组</a>
  <a href="#" onclick="return false;" id="msna"><u>更多</u> <small>▼</small></a>
  <div class="quick_menu" style="display:none;">
    <a target="_blank" href="http://job.iteye.com/iteye">招聘</a>
    <a href="http://www.iteye.com/search">搜索</a>
  </div>
</div>

      <div id="user_nav">
      <a href="http://tntxia.iteye.com/login" class="welcome" title="登录">您还未登录 !</a>
    <a href="http://tntxia.iteye.com/login">登录</a>
    <a href="http://tntxia.iteye.com/signup" class="nobg">注册</a>
  </div>

    </div>

    <div id="page">
      <div id="branding" class="clearfix">
        <div id="blog_name">
          <h1><a href="http://tntxia.iteye.com/">虾米博客</a></h1>
        </div>
        <div id="fd"></div>
        <div id="blog_navbar">
          <ul>
            <li class="blog_navbar_for"><a href="http://tntxia.iteye.com/"><strong>博客</strong></a></li>
            <li><a href="http://tntxia.iteye.com/weibo">微博</a></li>
            <li><a href="http://tntxia.iteye.com/album">相册</a></li>
            <li><a href="http://tntxia.iteye.com/link">收藏</a></li>
            <li><a href="http://tntxia.iteye.com/blog/guest_book">留言</a></li>
            <li><a href="http://tntxia.iteye.com/blog/profile">关于我</a></li>
          </ul>
    
          <div class="search">
            <form action="/blog/search" method="get">
              <input class="search_text" id="query" name="query" style="margin-left: 10px;width: 110px;" type="text">
              <input class="submit_search" value="" type="submit">
            </form>
          </div> 
          <div id="fd"></div>         
        </div>
      </div>
      
      <div id="content" class="clearfix">
        <div id="main">
          



          


<div class="blog_main">
  <div class="blog_title">
    <h3>
      <a href="http://tntxia.iteye.com/blog/682834">Swing文档大全 - Swing中树(Tree)的使用与介绍</a>
      <em class="actions">      </em>
    </h3>
    <ul class="blog_categories"><strong>博客分类：</strong> <li><a href="http://tntxia.iteye.com/category/17554">Java咖啡厅</a></li> </ul>
        <div class="news_tag"><a href="http://www.iteye.com/blogs/tag/Swing">Swing</a><a href="http://www.iteye.com/blogs/tag/F%23">F#</a><a href="http://www.iteye.com/blogs/tag/%E7%BD%91%E7%BB%9C%E5%BA%94%E7%94%A8">网络应用</a><a href="http://www.iteye.com/blogs/tag/XP">XP</a><a href="http://www.iteye.com/blogs/tag/%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86">项目管理</a>&nbsp;</div>
  </div>

  <div id="blog_content" class="blog_content">
    <p>10-1:使用JTree组件：<br>&nbsp;&nbsp; java.lang.Object<br>&nbsp;&nbsp;&nbsp;&nbsp; --java.awt.Component<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --java.awt.Container<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --javax.swing.JComponent<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --javax.swing.JTree<br>JTree构造函数:<br>JTree():建立一棵系统默认的树。<br>JTree(Hashtable value):利用Hashtable建立树，不显示root node(根节点).<br>JTree(Object[] value):利用Object Array建立树，不显示root node.<br>JTree(TreeModel newModel):利用TreeModel建立树。<br>JTree(TreeNode root):利用TreeNode建立树。<br>JTree(TreeNode root,boolean asksAllowsChildren):利用TreeNode建立树，并决定是否允许子节点的存在.<br>JTree(Vector value):利用Vector建立树，不显示root node.<br><br>范例:<br>InitalTree.java<br><br>import javax.swing.*;<br>import java.awt.*;<br>import java.awt.event.*;<br><br>public class InitalTree{<br>&nbsp; public InitalTree(){<br>&nbsp; JFrame f=new JFrame("TreeDemo");<br>&nbsp; Container contentPane=f.getContentPane();<br>&nbsp; <br>&nbsp; JTree tree=new JTree();<br>&nbsp; JScrollPane scrollPane=new JScrollPane();<br>&nbsp; scrollPane.setViewportView(tree);<br>&nbsp; <br>&nbsp; contentPane.add(scrollPane);<br>&nbsp; f.pack();<br>&nbsp; f.setVisible(true);<br>&nbsp; f.addWindowListener(new WindowAdapter(){<br>&nbsp; public void windowClosing(WindowEvent e){<br>&nbsp; &nbsp;&nbsp; System.exit(0); <br>&nbsp; }<br>&nbsp; });<br>&nbsp; } <br>&nbsp; public static void main(String[] args){<br>&nbsp; new InitalTree();<br>&nbsp; }<br>}<br><br>10-2:以Hashtable构造JTree:<br>&nbsp;&nbsp; 上面的例子对我们并没有裨的帮助，因为各个节点的数据均是java的默认值，而非我们自己设置的。因此我们需利用其他JTree<br>构造函数来输入我们想要的节点数据。以下范例我们以Hashtable当作JTree的数据输入:<br>范例：TreeDemo1.java<br><br>import javax.swing.*;<br>import java.awt.*;<br>import java.awt.event.*;<br>import java.util.*;<br><br>public class TreeDemo1{<br>&nbsp; public TreeDemo1(){<br>&nbsp;&nbsp;&nbsp; JFrame f=new JFrame("TreeDemo1");<br>&nbsp;&nbsp;&nbsp; Container contentPane=f.getContentPane();<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; String[] s1={"公司文件","个人信件","私人文件"}; <br>&nbsp;&nbsp;&nbsp; String[] s2={"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"};<br>&nbsp;&nbsp;&nbsp; String[] s3={"奇摩站","职棒消息","网络书店"};<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; Hashtable hashtable1=new Hashtable();<br>&nbsp;&nbsp;&nbsp; Hashtable hashtable2=new Hashtable();<br>&nbsp;&nbsp;&nbsp; hashtable1.put("我的公文包",s1);<br>&nbsp;&nbsp;&nbsp; hashtable1.put("我的电脑",s2);<br>&nbsp;&nbsp;&nbsp; hashtable1.put("收藏夹",hashtable2);<br>&nbsp;&nbsp;&nbsp; hashtable2.put("网站列表",s3);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; Font font = new Font("Dialog", Font.PLAIN, 12);<br>&nbsp;&nbsp;&nbsp; Enumeration keys = UIManager.getLookAndFeelDefaults().keys();<br>&nbsp;&nbsp; /**定义widnows界面**/<br>&nbsp;&nbsp;&nbsp; while (keys.hasMoreElements()) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object key = keys.nextElement();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (UIManager.get(key) instanceof Font) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.put(key, font);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; } <br>&nbsp;&nbsp; try{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");&nbsp; <br>&nbsp;&nbsp; }catch(Exception el){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);&nbsp; <br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; /**定义widnows界面**/<br>&nbsp;&nbsp;&nbsp; JTree tree=new JTree(hashtable1);<br>&nbsp;&nbsp;&nbsp; JScrollPane scrollPane=new JScrollPane();<br>&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp; contentPane.add(scrollPane);<br>&nbsp; f.pack();<br>&nbsp; f.setVisible(true);<br>&nbsp; f.addWindowListener(new WindowAdapter(){<br>&nbsp; public void windowClosing(WindowEvent e){<br>&nbsp; &nbsp;&nbsp; System.exit(0); <br>&nbsp; }<br>&nbsp; });<br>&nbsp; } <br>&nbsp; public static void main(String[] args){<br>&nbsp; new TreeDemo1();<br>&nbsp; }<br>}<br><br>纯XP界面的设置：<br><br>10-3:以TreeNode构造JTree:<br>&nbsp;&nbsp; JTree上的每一个节点就代表一个TreeNode对象，TreeNode本身是一个Interface,里面定义了7个有关节点的方法，例如判断是否<br>为树叶节点、有几个子节点(getChildCount())、父节点为何(getparent())等等、这些方法的定义你可以在javax.swing.tree的<br>package中找到，读者可自行查阅java api文件。在实际的应用上，一般我们不会直接实作此界面，而是采用java所提供的<br>DefaultMutableTreeMode类，此类是实作MutableTreeNode界面而来，并提供了其他许多实用的方法。MutableTreeNode本身也是一<br>个Interface,且继承了TreeNode界面此类主要是定义一些节点的处理方式，例如新增节点(insert())、删除节点(remove())、设置<br>节点(setUserObject())等。整个关系如下图:<br>&nbsp;&nbsp;&nbsp; TreeNode----extends---&gt;MutableTreeNode---implements---DefaultMutableTreeNode<br><br>&nbsp; 接下来我们来看如何利DefaultMutableTreeNode来建立JTree,我们先来看DefaultMutableTreeNode的构造函数:<br><br>DefaultMutableTreeNode构造函数:<br>DefaultMutableTreeNode():建立空的DefaultMutableTreeNode对象。<br>DefaultMutableTreeNode(Object userObject):建立DefaultMutableTreeNode对象，节点为userObject对象。<br>DefaultMutableTreeNode(Object userObject,Boolean allowsChildren):建立DefaultMutableTreeNode对象，节点为userObject对<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 象并决定此节点是否允许具有子节点。<br>&nbsp; 以下为利用DefaultMutableTreeNode建立JTree的范例:TreeDemo2.java<br>&nbsp;&nbsp;&nbsp; 此程序"资源管理器"为此棵树的根节点.<br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.tree.*;<br>public class TreeDemo2{<br>&nbsp; public TreeDemo2(){<br>&nbsp;&nbsp;&nbsp; JFrame f=new JFrame("TreeDemo2");<br>&nbsp;&nbsp;&nbsp; Container contentPane=f.getContentPane();<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器"); <br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node1=new DefaultMutableTreeNode("我的公文包");<br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node2=new DefaultMutableTreeNode("我的电脑");<br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node3=new DefaultMutableTreeNode("收藏夹");<br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node4=new DefaultMutableTreeNode("Readme");<br>&nbsp;&nbsp;&nbsp; root.add(node1);<br>&nbsp;&nbsp;&nbsp; root.add(node2);<br>&nbsp;&nbsp;&nbsp; root.add(node3);<br>&nbsp;&nbsp;&nbsp; root.add(node4);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("公司文件");<br>&nbsp;&nbsp;&nbsp; node1.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("私人文件");<br>&nbsp;&nbsp;&nbsp; node1.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("个人信件");<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("本机磁盘(C:)");<br>&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("本机磁盘(D:)");<br>&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("本机磁盘(E:)");<br>&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node31=new DefaultMutableTreeNode("网站列表");<br>&nbsp;&nbsp;&nbsp; node3.add(node31);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("奇摩站");<br>&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("职棒消息");<br>&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp; leafnode=new DefaultMutableTreeNode("网络书店");<br>&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; JTree tree=new JTree(root);<br>&nbsp;&nbsp;&nbsp; JScrollPane scrollPane=new JScrollPane();<br>&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane);<br>&nbsp; f.pack();<br>&nbsp; f.setVisible(true);<br>&nbsp; f.addWindowListener(new WindowAdapter(){<br>&nbsp; public void windowClosing(WindowEvent e){<br>&nbsp; &nbsp;&nbsp; System.exit(0); <br>&nbsp; }<br>&nbsp; });<br>&nbsp; } <br>&nbsp; public static void main(String[] args){<br>&nbsp; new TreeDemo2();<br>&nbsp; }&nbsp; <br>}<br><br>10-4:以TreeModel构造JTree.<br>&nbsp;&nbsp; 除了以节点的观念(TreeNode)建立树之外，你可以用data model的模式建立树。树的data model称为TreeModel,用此模式的好处<br>是可以触发相关的树事件，来处理树可能产生的一些变动。TreeModel是一个interface,里面定义了8种方法;如果你是一个喜欢自己<br>动手做的人，或是你想显示的数据格式很复杂，你可以考虑直接实作TreeModel界面中所定义的方法来构造出JTree.TreeModel界面<br>的方法如下所示:<br>TreeModel方法:<br>void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addTreeModelListener(TreeModelListener l):增加一个TreeModelListener来监控TreeModelEvent事件。<br>Object&nbsp;&nbsp;&nbsp; getChild(Object parent,int index):返回子节点。<br>int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getChildCount(Object parent):返回子节点数量.<br>int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getIndexOfChild(Object parent,Object child):返回子节点的索引值。<br>Object&nbsp;&nbsp;&nbsp; getRoot():返回根节点。<br>boolean&nbsp;&nbsp; isLeaf(Object node):判断是否为树叶节点。<br>void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removeTreeModelListener(TreeModelListener l):删除TreeModelListener。<br>void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; valueForPathChanged(TreePath path,Object newValue):当用户改变Tree上的值时如何应对。<br><br>&nbsp;&nbsp; 你可以实作出这8种方法，然后构造出自己想要的JTree,不过在大部份的情况下我们通常不会这样做，毕竟要实作出这8种方法不<br>是件很轻松的事，而且java本身也提供了一个默认模式，叫做DefaultTreeModel,这个类已经实作了TreeModel界面，也另外提供许<br>多实用的方法。利用这个默认模式，我们便能很方便的构造出JTree出来了。下面为DefaultTreeModel的构造函数与范例:<br>DefaultTreeModel构造函数:<br>DefaultTreeModel(TreeNode root):建立DefaultTreeModel对象，并定出根节点。<br>DefaultTreeModel(TreeNode root,Boolean asksAllowsChildren):建立具有根节点的DefaultTreeModel对象，并决定此节点是否允<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 许具有子节点。<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.tree.*;<br>import com.incors.plaf.alloy.*;//组件的下载网址http://www.incors.com/lookandfeel/<br>/*将alloy.jar放在c:\j2sdk1.4.0\jre\lib\ext\目录下.<br>*/<br>public class TreeDemo3<br>{<br>&nbsp;&nbsp;&nbsp; public TreeDemo3()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame f = new JFrame("TreeDemo");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Container contentPane = f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultTreeModel treeModel = new DefaultTreeModel(root);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*DefaultTreeModel类所提供的insertNodeInto()方法加入节点到父节点的数量.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *利用DefaultMutableTreeNode类所提供的getChildCount()方法取得目前子节点的数量.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node1, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node2, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node3, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node4, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode leafnode = new <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode("公司文件");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //DefaultTreeModel类所提供的insertNodeInto()方法加入节点到父节点的数量.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("个人信件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("私人文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node31, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("奇摩站");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("职棒消息");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("网络书店");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel();&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (UnsupportedLookAndFeelException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // You may handle the exception here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this line needs to be implemented in order to make JWS work properly<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 UIManager.getLookAndFeelDefaults().put("ClassLoader", 
getClass().getClassLoader());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //以TreeModel建立JTree。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = new JTree(treeModel);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*改变JTree的外观**/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.putClientProperty("JTree.lineStyle","Horizontal");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*改变JTree的外观**/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane = new JScrollPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br><br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public static void main(String args[]) {<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo3();<br>&nbsp;&nbsp;&nbsp; }<br>}<br>10-5:改变JTree的外观:<br>你可以使用JComponent所提供的putClientProperty(Object key,Object value)方法来设置java默认的JTree外观，设置方式共有<br>3种:<br>1.tree.putClientProperty("JTree.lineStyle","None"):java默认值。<br>2.tree.putClientProperty("JTree.lineStyle","Horizontal"):使JTree的文件夹间具有水平分隔线。<br>3.tree.putClientProperty("JTree.lineStyle","Angled"):使JTree具有类似Windows文件管理器的直角连接线。<br>&nbsp; 具体怎样做，可看上例.<br><br>10-6:更换JTree节点图案:<br>&nbsp; JTree利用TreeCellRenderer界面来运行绘制节点的工作，同样的，你不需要直接支实作这个界面所定义的方法，因为java本身提<br>供一个已经实作好的类来给我们使用，此类就是DefaultTreeCellRenderer,你可以在javax.swing.tree package中找到此类所提供<br>的方法。下面为使用DefaultTreeCellRenderer更改节点图案的一个例子:<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.tree.*;<br>import com.incors.plaf.alloy.*;<br><br>public class TreeDemo4{<br>&nbsp;&nbsp; public TreeDemo4(){<br>&nbsp;&nbsp;&nbsp;&nbsp; JFrame f=new JFrame("TreeDemo");<br>&nbsp;&nbsp;&nbsp;&nbsp; Container contentPane=f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultTreeModel treeModel = new DefaultTreeModel(root);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node1, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node2, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node3, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node4, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode leafnode = new <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode("公司文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("个人信件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("私人文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node31, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("奇摩站");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("职棒消息");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("网络书店");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel();&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (UnsupportedLookAndFeelException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // You may handle the exception here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this line needs to be implemented in order to make JWS work properly<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 UIManager.getLookAndFeelDefaults().put("ClassLoader", 
getClass().getClassLoader());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = new JTree(treeModel);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.setRowHeight(20);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)tree.getCellRenderer();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setLeafIcon(new ImageIcon("..\\icons\\leaf.gif"));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setOpenIcon(new ImageIcon("..\\icons\\open.gif"));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setClosedIcon(new ImageIcon("..\\icons\\close.gif"));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));//设置字体.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setBackgroundNonSelectionColor(Color.white);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setBackgroundSelectionColor(Color.yellow);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setBorderSelectionColor(Color.red);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*设置选时或不选时，文字的变化颜色<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setTextNonSelectionColor(Color.black);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellRenderer.setTextSelectionColor(Color.blue);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane=new JScrollPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br><br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public static void main(String args[]) {<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo4();<br>&nbsp;&nbsp;&nbsp; }<br>}<br>Window Xp界面:<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.tree.*;<br>import com.incors.plaf.alloy.*;<br>public class TreeDemo3<br>{<br>&nbsp;&nbsp;&nbsp; public TreeDemo3()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //设置成Alloy界面样式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", 
"true");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel();&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame.setDefaultLookAndFeelDecorated(true);&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (UnsupportedLookAndFeelException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // You may handle the exception here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this line needs to be implemented in order to make JWS work properly<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.getLookAndFeelDefaults().put("ClassLoader", getClass().getClassLoader());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //JDialog.setDefaultLookAndFeelDecorated(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame f = new JFrame("firstTree");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Container contentPane = f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // if (contentPane instanceof JComponent) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp; ((JComponent) contentPane).setMinimumSize(new Dimension(100, 100));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Container contentPane = f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultTreeModel treeModel = new DefaultTreeModel(root);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node1, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node2, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node3, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node4, root, root.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode leafnode = new <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode("公司文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("个人信件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("私人文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(node31, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("奇摩站");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("职棒消息");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("网络书店");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = new JTree(treeModel);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*改变JTree的外观**/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; tree.putClientProperty("JTree.lineStyle","Horizontal");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*改变JTree的外观**/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane = new JScrollPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br><br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public static void main(String args[]) {<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo3();<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br><br>10-7:JTree的事件处理模式:<br>&nbsp;&nbsp;&nbsp; 在此节中，我们将详细介绍JTree两个常用的事件与处理，分别是TreeModeEvent与TreeSelectionEvent.<br>10-7-1:处理TreeModeEvent事件：<br>&nbsp; 当树的结构上有任何改变时，例如节点值改变了、新增节点、删除节点等，都会TreeModelEvent事件，要处理这样的事件必须实<br>作TreeModelListener界面，此界面定义了4个方法，如下所示:<br>TreeModelListener方法:<br>Void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 treeNodesChanged(TreeModelEvent e):当节点改变时系统就会云调用这个方法。<br>Void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 treeNodesInserted(TreeModelEvent e):当新增节时系统就会去调用这个方法。<br>Void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 treeNodesRemoved(TreeModeEvent e):当删除节点时系统就会去调用这个方法。<br>Void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 treeStructureChanged(TreeModelEvent e):当树结构改变时系统就会去调用这个方法。<br><br>&nbsp; TreeModelEvent类本身提供了5个方法，帮我们取得事件的信息，如下所示:<br><br>TreeModelEvent方法：<br>int[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getChildIndices():返回子节点群的索引值。<br>Object[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getChildren():返回子节点群.<br>Object[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 getPath():返回Tree中一条path上(从root nod到leaf node)的节点。<br>TreePath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getTreePath():取得目前位置的Tree Path.<br>String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; toString()：取得蝗字符串表示法.<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; 由TreeModelEvent的getTreePath()方法就可以得到TreePath对象，此对象就能够让我们知道用户目前正选哪一个节点，<br>TreePath类最常用的方法为：<br>&nbsp;&nbsp;&nbsp;&nbsp; public&nbsp; Object getLastPathComponent():取得最深（内）层的节点。<br>&nbsp;&nbsp;&nbsp;&nbsp; public int&nbsp;&nbsp;&nbsp; getPathCount():取得此path上共有几个节点.<br>&nbsp;&nbsp;&nbsp; 我们来看下面这个例子，用户可以在Tree上编辑节点，按下[Enter]键后就可以改变原有的值，并将改变的值显示在下面的<br>JLabel中:<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.event.*;<br>import javax.swing.tree.*;<br>import com.incors.plaf.alloy.*;<br>import com.incors.plaf.alloy.themes.bedouin.*;<br><br>public class TreeDemo5 implements TreeModelListener<br>{<br>&nbsp;&nbsp;&nbsp; JLabel label = null;<br>&nbsp;&nbsp;&nbsp; String nodeName = null; //原有节点名称<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public TreeDemo5()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyTheme theme = new BedouinTheme();//设置界面的外观，手册中共有5种样式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (UnsupportedLookAndFeelException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // You may handle the exception here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this line needs to be implemented in order to make JWS work properly<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.getLookAndFeelDefaults().put("ClassLoader", getClass().getClassLoader());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame f = new JFrame("TreeDemo");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Container contentPane = f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("文件夹");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node3);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node4);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node1.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("个人信件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node1.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("私人文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node1.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node2.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node3.add(node31);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("天勤网站");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("足球消息");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leafnode = new DefaultMutableTreeNode("网络书店");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node31.add(leafnode);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = new JTree(root);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.setEditable(true);//设置JTree为可编辑的<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.addMouseListener(new MouseHandle());//使Tree加入检测Mouse事件，以便取得节点名称 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面两行取得DefaultTreeModel,并检测是否有TreeModelEvent事件.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultTreeModel treeModel = (DefaultTreeModel)tree.getModel();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeModel.addTreeModelListener(this);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane = new JScrollPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label = new JLabel("更改数据为: ");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane,BorderLayout.CENTER);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(label,BorderLayout.SOUTH);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br><br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; /*本方法实作TreeModelListener接口，本接口共定义四个方法，分别是TreeNodesChanged()<br>&nbsp;&nbsp;&nbsp;&nbsp; *treeNodesInserted()、treeNodesRemoved()、treeNodesRemoved()、<br>&nbsp;&nbsp;&nbsp;&nbsp; *treeStructureChanged().在此范例中我们只针对更改节点值的部份，因此只实作<br>&nbsp;&nbsp;&nbsp;&nbsp; *treeNodesChanged()方法.<br>&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; public void treeNodesChanged(TreeModelEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreePath treePath = e.getTreePath();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(treePath);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面这行由TreeModelEvent取得的DefaultMutableTreeNode为节点的父节点，而不是用户点选<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //的节点，这点读者要特别注意。要取得真正的节点需要再加写下面6行代码.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 DefaultMutableTreeNode node = 
(DefaultMutableTreeNode)treePath.getLastPathComponent();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 //getChildIndices()方法会返回目前修改节点的索引值。由于我们只修改一个节点，因此节点索引值就放在index[0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 //的位置，若点选的节点为root node,则getChildIndices()的返回值为null,程序下面的第二行就在处理点选root<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //node产生的NullPointerException问题.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int[] index = e.getChildIndices();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 //由DefaultMutableTreeNode类的getChildAt()方法取得修改的节点对象.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node = (DefaultMutableTreeNode)node.getChildAt(index[0]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (NullPointerException exc) {}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //由DefaultMutableTreeNode类getUserObject()方法取得节点的内容，或是写成node.toString()亦相同.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label.setText(nodeName+"更改数据为: "+(String)node.getUserObject());<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; public void treeNodesInserted(TreeModelEvent e) {<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; public void treeNodesRemoved(TreeModelEvent e) {<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; public void treeStructureChanged(TreeModelEvent e) {<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public static void main(String args[]) {<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo5();<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; //处理Mouse点选事件<br>&nbsp;&nbsp;&nbsp; class MouseHandle extends MouseAdapter<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void mousePressed(MouseEvent e) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = (JTree)e.getSource();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //JTree的getRowForLocation()方法会返回节点的列索引值。例如本例中，“本机磁盘(D:)”的列索引值为4,此索引值<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //会随着其他数据夹的打开或收起而变支，但“资源管理器”的列索引值恒为0. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 int rowLocation = tree.getRowForLocation(e.getX(), e.getY());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 /*JTree的getPathForRow()方法会取得从root node到点选节点的一条path,此path为一条直线，如程序运行的图示<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 *若你点选“本机磁盘(E:)”,则Tree 
Path为"资源管理器"--&gt;"我的电脑"--&gt;"本机磁盘(E:)",因此利用TreePath<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *的getLastPathComponent()方法就可以取得所点选的节点.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 TreePath treepath = tree.getPathForRow(rowLocation);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 TreeNode treenode = (TreeNode) treepath.getLastPathComponent(); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodeName = treenode.toString();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(NullPointerException ne){}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>注:上面的程序MouseHandle中:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 int rowLocation = tree.getRowForLocation(e.getX(), e.getY());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 TreePath treepath = tree.getPathForRow(rowLocation);<br>&nbsp;&nbsp; 与：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreePath treepath=tree.getSelectionPath();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 等价，可互换。<br>&nbsp; <br>&nbsp; 我们将“我的电脑”改成“网上领居”:<br>&nbsp; 我们再来看一个TreeModelEvent的例子，下面这个例子我们可以让用户自行增加、删除与修改节点：<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.event.*;<br>import javax.swing.tree.*;<br>import com.incors.plaf.alloy.*;<br>import com.incors.plaf.alloy.themes.bedouin.*;<br><br>public class TreeDemo6 implements ActionListener,TreeModelListener{<br>&nbsp; JLabel label=null;<br>&nbsp; JTree&nbsp; tree=null;<br>&nbsp; DefaultTreeModel treeModel=null;<br>&nbsp; String nodeName=null;//原有节点名称<br>&nbsp; <br>&nbsp; public TreeDemo6(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyTheme theme = new BedouinTheme();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (UnsupportedLookAndFeelException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // You may handle the exception here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this line needs to be implemented in order to make JWS work properly<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.getLookAndFeelDefaults().put("ClassLoader", getClass().getClassLoader());<br>&nbsp;&nbsp;&nbsp; JFrame f=new JFrame("TreeDemo6");<br>&nbsp;&nbsp;&nbsp; Container contentPane=f.getContentPane();<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器"); <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; tree=new JTree(root);<br>&nbsp;&nbsp;&nbsp; tree.setEditable(true);<br>&nbsp;&nbsp;&nbsp; tree.addMouseListener(new MouseHandle());<br>&nbsp;&nbsp;&nbsp; treeModel=(DefaultTreeModel)tree.getModel();<br>&nbsp;&nbsp;&nbsp; treeModel.addTreeModelListener(this);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; JScrollPane scrollPane=new JScrollPane();<br>&nbsp;&nbsp;&nbsp; scrollPane.setViewportView(tree);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; JPanel panel=new JPanel();<br>&nbsp;&nbsp;&nbsp; JButton b=new JButton("新增节点");<br>&nbsp;&nbsp;&nbsp; b.addActionListener(this);<br>&nbsp;&nbsp;&nbsp; panel.add(b);<br>&nbsp;&nbsp;&nbsp; b=new JButton("删除节点");<br>&nbsp;&nbsp;&nbsp; b.addActionListener(this);<br>&nbsp;&nbsp;&nbsp; panel.add(b);<br>&nbsp;&nbsp;&nbsp; b=new JButton("清除所有节点");<br>&nbsp;&nbsp;&nbsp; b.addActionListener(this);<br>&nbsp;&nbsp;&nbsp; panel.add(b);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; label=new JLabel("Action");<br>&nbsp;&nbsp;&nbsp; contentPane.add(panel,BorderLayout.NORTH);<br>&nbsp;&nbsp;&nbsp; contentPane.add(scrollPane,BorderLayout.CENTER);<br>&nbsp;&nbsp;&nbsp; contentPane.add(label,BorderLayout.SOUTH);<br>&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter(){<br>&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e){<br>&nbsp;&nbsp;&nbsp; &nbsp; System.exit(0); <br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; });<br>&nbsp; } <br>&nbsp; //本方法运行新增、删除、清除所有节点的程序代码.<br>&nbsp; public void actionPerformed(ActionEvent ae){<br>&nbsp; if (ae.getActionCommand().equals("新增节点")){<br>&nbsp; DefaultMutableTreeNode parentNode=null;<br>&nbsp; DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");<br>&nbsp; newNode.setAllowsChildren(true);<br>&nbsp; TreePath parentPath=tree.getSelectionPath();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //取得新节点的父节点<br>&nbsp; parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //由DefaultTreeModel的insertNodeInto（）方法增加新节点<br>&nbsp; treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //会被 包在文件夹中，你必须自行展开文件夹才看得到。<br>&nbsp; tree.scrollPathToVisible(new TreePath(newNode.getPath()));&nbsp; <br>&nbsp; label.setText("新增节点成功");<br>&nbsp; }<br>&nbsp; if (ae.getActionCommand().equals("删除节点")){<br>&nbsp; TreePath treepath=tree.getSelectionPath();<br>&nbsp; if (treepath!=null){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面两行取得选取节点的父节点.<br>&nbsp; &nbsp; DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();<br>&nbsp; &nbsp; TreeNode parent=(TreeNode)selectionNode.getParent();<br>&nbsp; &nbsp; if (parent!=null) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 //由DefaultTreeModel的removeNodeFromParent()方法删除节点，包含它的子节点。<br>&nbsp; &nbsp; treeModel.removeNodeFromParent(selectionNode);<br>&nbsp; &nbsp; label.setText("删除节点成功");<br>&nbsp; &nbsp; }<br>&nbsp; }&nbsp; <br>&nbsp; }<br>&nbsp; if (ae.getActionCommand().equals("清除所有节点")){<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面一行，由DefaultTreeModel的getRoot()方法取得根节点.<br>&nbsp; DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面一行删除所有子节点.<br>&nbsp; rootNode.removeAllChildren();&nbsp;&nbsp; <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //删除完后务必运行DefaultTreeModel的reload()操作，整个Tree的节点才会真正被删除.&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; treeModel.reload();<br>&nbsp; label.setText("清除所有节点成功");<br>&nbsp; }<br>&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; public void treeNodesChanged(TreeModelEvent e){<br>&nbsp; &nbsp;&nbsp; TreePath treePath=e.getTreePath();<br>&nbsp; &nbsp;&nbsp; DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();<br>&nbsp; &nbsp;&nbsp; try{<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; int[] index=e.getChildIndices();<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; node=(DefaultMutableTreeNode)node.getChildAt(index[0]); <br>&nbsp; &nbsp;&nbsp; }catch(NullPointerException exc){}<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; label.setText(nodeName+"更改数据为:"+(String)node.getUserObject());<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp; public void treeNodesInserted(TreeModelEvent e){<br>&nbsp; &nbsp;&nbsp; System.out.println("new node insered");&nbsp; &nbsp;&nbsp; <br>&nbsp; }<br>&nbsp; public void treeNodesRemoved(TreeModelEvent e){<br>&nbsp; &nbsp;&nbsp; System.out.println("node deleted"); <br>&nbsp; }<br>&nbsp; public void treeStructureChanged(TreeModelEvent e){<br>&nbsp; &nbsp;&nbsp; System.out.println("Structrue changed"); <br>&nbsp; }<br>&nbsp;&nbsp;&nbsp; public static void main(String[] args){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo6(); <br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; class MouseHandle extends MouseAdapter{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void mousePressed(MouseEvent e){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree=(JTree)e.getSource();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int rowLocation=tree.getRowForLocation(e.getX(),e.getY());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreePath treepath=tree.getPathForRow(rowLocation); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreeNode treenode=(TreeNode)treepath.getLastPathComponent();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
nodeName=treenode.toString();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(NullPointerException ne){}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>&nbsp;&nbsp;&nbsp; }<br>} <br><br>10-7-2：处理TreeSelectionEvent事件：<br>&nbsp;&nbsp;&nbsp; 当我们在JTree上点选任何一个节点，都会触发TreeSelectionEvent事件，如果我们要处理这样的事件，必须实作<br>TreeSelectionListener界面，此界面只定义了一个方法，那就是valueChanged()方法。<br>&nbsp;&nbsp;&nbsp; TreeSelectionEvent最常用在处理显示节点的内容，例如你在文件图标中点两下就可以看到文件的内容。在JTree中选择节点<br>的方式共有3种，这3种情况跟选择JList上的项目是一模一样的，分别是：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DISCONTIGUOUS_TREE_SELECTION:可作单一选择，连续点选择（按住[Shift]键），不连续选择多个节点（按住[Ctrl]键）,<br>这是java默认值.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONTINUOUS_TREE_SELECTION:按住[Shift]键，可对某一连续的节点区间作选取。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SINGLE_TREE_SELECTION:一次只能选一个节点。<br>&nbsp;&nbsp; 你可以自行实作TreeSelectionModel制作作更复杂的选择方式，但通常是没有必要的，因为java提供了默认的选择模式类供我们<br>使用，那就是DefaultTreeSelectionModel,利用这个类我们可以很方便的设置上面3种选择模式。<br>&nbsp;&nbsp; 下面这个范例，当用户点选了一个文件名时，就会将文件的内容显示出来。<br>TreeDemo7.java<br><br>import javax.swing.*;<br>import javax.swing.tree.*;<br>import javax.swing.event.*;<br>import java.awt.*;<br>import java.awt.event.*;<br>import java.io.*;<br>import java.util.*;<br>import com.incors.plaf.alloy.*;<br>import com.incors.plaf.alloy.themes.glass.*;<br><br>public class TreeDemo7 implements TreeSelectionListener<br>{<br>&nbsp;&nbsp;&nbsp; JEditorPane editorPane;<br><br>&nbsp;&nbsp;&nbsp; public TreeDemo7()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame f = new JFrame("TreeDemo");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Container contentPane = f.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode node = new DefaultMutableTreeNode("TreeDemo1.java");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node = new DefaultMutableTreeNode("TreeDemo2.java");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node = new DefaultMutableTreeNode("TreeDemo3.java");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node = new DefaultMutableTreeNode("TreeDemo4.java");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.add(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = new JTree(root);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //设置Tree的选择模式为一次只能选择一个节点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //检查是否有TreeSelectionEvent事件。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tree.addTreeSelectionListener(this);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //下面五行，JSplitPane中，左边是放含有JTree的JScrollPane,右边是放JEditorPane.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane1 = new JScrollPane(tree);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; editorPane = new JEditorPane();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JScrollPane scrollPane2 = new JScrollPane(editorPane);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JSplitPane splitPane = new JSplitPane(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JSplitPane.HORIZONTAL_SPLIT,true, scrollPane1, scrollPane2);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contentPane.add(splitPane);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.addWindowListener(new WindowAdapter() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; //本方法实作valueChanged()方法<br>&nbsp;&nbsp;&nbsp; public void valueChanged(TreeSelectionEvent e)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JTree tree = (JTree) e.getSource(); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode selectionNode =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String nodeName = selectionNode.toString();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //判断是否为树叶节点，若是则显示文件内容，若不是则不做任何事。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (selectionNode.isLeaf()) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*取得文件的位置路径,System.getProperty("user.dir")可以取得目前工作的路径，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *System.getProperty("file.separator")是取得文件分隔符，例如在window环境的<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *文件分陋符是"\",而Unix环境的文件分隔符刚好相反，是"/".利用System.getProperty()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *方法你可以取得下列的信息:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java版本<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.endor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java制造商<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.endor.url&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java制造商URL<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.home&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java的安装路径<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.class.version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java类版本<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
java.class.path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示java classpath<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
os.name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示操作系统名称<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
os.arch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示操作系统结构，如x86<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
os.version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 显示操作系统版本<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
file.separator&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得文件分隔符<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
path.separator&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得路径分隔符，如Unix是以“:”表示<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
line.separator&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得换行符号，如Unix是以"\n"表示<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
user.name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得用户名称<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
user.home&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得用户家目录(home directory),如Windows中Administrator的家目<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 录为c:\Documents and Settings\Administrator<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
user.dir&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 取得用户目前的工作目录.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String filepath = "file:"+System.getProperty("user.dir") +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 System.getProperty("file.separator") +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 nodeName;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 //利用JEditorPane的setPage()方法将文件内容显示在editorPane中。若文件路径错误，则会产生IOException.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; editorPane.setPage(filepath);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch(IOException ex) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("找不到此文件");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br>&nbsp;&nbsp;&nbsp; SwingUtil.setLookAndFeel();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new TreeDemo7();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>class SwingUtil{<br>&nbsp; public static final void setLookAndFeel() {<br>&nbsp; try{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Font font = new Font("JFrame", Font.PLAIN, 12);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enumeration keys = UIManager.getLookAndFeelDefaults().keys();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (keys.hasMoreElements()) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object key = keys.nextElement();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (UIManager.get(key) instanceof Font) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.put(key, font);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlloyTheme theme = new GlassTheme();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame.setDefaultLookAndFeelDecorated(true); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UIManager.setLookAndFeel(alloyLnF);<br>&nbsp; }catch(UnsupportedLookAndFeelException ex){<br>&nbsp; &nbsp; ex.printStackTrace(); <br>&nbsp; }<br>}<br>}<br><br>10-8:JTree的其他操作:<br>&nbsp;&nbsp;&nbsp;&nbsp; 我们在之前小节中曾说到Tree中的每一个节点都是一个TreeNode,并可利用JTree的setEditable()方法设置节点是否可编辑，<br>若要在Tree中找寻节点的父节点或子节点，或判断是否为树节点，皆可由实作TreeNode界面做到，但要编辑节点呢？java将编辑<br>节点的任务交给TreeCellEditor,TreeCellEditor本身是一个界面，里面只定义了getTreeCellEditor Component()方法，你可以实<br>作此方法使节点具有编辑的效果。不过你不用这么辛苦去实作这个方法，java本身提供了DefaultTreeCellEditor类来实作此方法<br>，亦提供了其他许多方法，例如取得节点内容(getCellEditorValue()) 、设置节点字体(setFont())、决定节点是否可编辑<br>(isCellEditable())等等。除非你觉得DefaultTreeCellEditor所提供的功能不够，你才需要去实作TreeCellEditor界面。你可以利<br>用JTree的getCellEditor()方法取得DefaultTreeCellEditor对象。当我们编辑节点时会触发ChangeEvent事件，你可以实作<br>CellEditorListener界面来处理此事件，CellEditorListener界面包括两个方法，分别是editingStopped(ChangeEvent e)与<br>editingCanceled(ChangeEvent e).若你没有实作TreeCellEditor界面，系统会以默认的DefaultTreeCellEdtior类来处理掉这两个<br>方法(你可以在DefaultTreeCellEditor中找到这两个方法)，因此你无须再编写任何的程序。<br>&nbsp;&nbsp;&nbsp;&nbsp; 另外，JTree还有一种事件处理模式，那就是TreeExpansionEvent事件。要处理这个事件你必须实作TreeExpansionListener<br>界面，此界面定义了两个方法，分别是treeCollapsed(TreeExpansionEvent e)与treeExpanded(TreeExpansionEvent e).当节点展<br>开时系统就会自动调用treeExpanded()方法，当节点合起来时，系统就会自动调用treeCollapsed()方法。你可以在这两个方法中编<br>写所要处理事情的程序代码。处理事件的过程我们已在上面举过多次，这里就不再重复了，可参考下面的网站：<br>&nbsp;&nbsp;&nbsp; http://javaalmanac.com/egs/index.html</p>
  </div>

  


  <iframe src="682834_files/794.htm" frameborder="0" height="60" scrolling="no" width="468"></iframe>
  
  <div id="bottoms" class="clearfix">
    
    <div id="share_weibo">分享到：
      <a data-type="sina" href="javascript:;" title="分享到新浪微博"><img src="682834_files/sina.jpg"></a>
      <a data-type="qq" href="javascript:;" title="分享到腾讯微博"><img src="682834_files/tec.jpg"></a>
    </div>
  </div>

  <div class="blog_nav">
    <div class="pre_next">
      <a href="http://tntxia.iteye.com/blog/682855" class="next" title="关于Javaeye的碎碎念">关于Javaeye的碎碎念</a>
      |
      <a href="http://tntxia.iteye.com/blog/679298" class="pre" title="Q宠社区寻宝图">Q宠社区寻宝图</a>
    </div>
  </div>
  <div class="blog_bottom">
    <ul>
      <li>2010-06-03 11:26</li>
      <li>浏览 2318</li>
      <li><a href="#comments">评论(1)</a></li>
      
      
      <li>分类:<a href="http://www.iteye.com/blogs/category/language">编程语言</a></li>      
      <li class="last"><a href="http://www.iteye.com/wiki/blog/682834" target="_blank" class="more">相关推荐</a></li>
    </ul>
  </div>

  <div class="blog_comment">
    <h5>评论</h5>
    <a id="comments" name="comments"></a>
    <div id="bc2297561">
  <div class="comment_title">
    1 楼
    <a href="http://gussofia.iteye.com/" target="_blank" title="gussofia">gussofia</a>
    2013-01-15&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content">hao&nbsp; xiexie </div>
</div>


    
    
  </div>

  <div class="blog_comment">
    <h5>发表评论</h5>
            <p style="text-align:center; margin-top:30px;margin-bottom:0px;"><a href="http://tntxia.iteye.com/login" style="background-color:white;"> <img src="682834_files/login_icon.png" style="vertical-align:middle; margin-right: 10px;"></a><a href="http://tntxia.iteye.com/login">  您还没有登录,请您登录后再发表评论 </a></p>
      </div>
</div>


<script type="text/javascript">
  dp.SyntaxHighlighter.HighlightAll('code', true, true);

  $$('#main .blog_content pre[name=code]').each(function(pre, index){ // blog content
    var post_id = 682834;
    var location = window.location;
    source_url = location.protocol + "//" + location.host + location.pathname + location.search;
    pre.writeAttribute('codeable_id', post_id);
    pre.writeAttribute('codeable_type', "Blog");
    pre.writeAttribute('source_url', source_url);
    pre.writeAttribute('pre_index', index);
    pre.writeAttribute('title', 'Swing文档大全 - Swing中树(Tree)的使用与介绍');
  });

  fix_image_size($$('div.blog_content img'), 700);

  function processComment() {
    $$('#main .blog_comment > div').each(function(comment){// comment
      var post_id = comment.id.substr(2);
      $$("#"+comment.id+" pre[name=code]").each(function(pre, index){
        var location = window.location;
        source_url = location.protocol + "//" + location.host + location.pathname + location.search;
        source_url += "#" + comment.id;
        pre.writeAttribute('codeable_id', post_id);
        pre.writeAttribute('codeable_type', "BlogComment");
        pre.writeAttribute('source_url', source_url);
        pre.writeAttribute('pre_index', index);
        pre.writeAttribute('title', 'Swing文档大全 - Swing中树(Tree)的使用与介绍');
      });
    });
  }

  function quote_comment(id) {
    new Ajax.Request('/editor/quote', {
      parameters: {'id':id, 'type':'BlogComment'},
      onSuccess:function(response){editor.bbcode_editor.textarea.insertAfterSelection(response.responseText);
        Element.scrollTo(editor.bbcode_editor.textarea.element);}
    });
  }

  code_favorites_init();
  processComment();
  new WeiboShare({share_buttons: $('share_weibo'), img_scope: $('blog_content')});
</script>




        </div>

        <div id="local">
          <div class="local_top"></div>
          <div id="blog_owner">
  <div id="blog_owner_logo"><a href="http://tntxia.iteye.com/"><img alt="tntxia的博客" class="logo" src="682834_files/bd274246-2586-3c1a-b3dd-bc39d7822242.jpg" title="tntxia的博客: 虾米博客"></a></div>
  <div id="blog_owner_name">tntxia</div>
</div>

          <div id="blog_actions">
            <ul>
              <li>浏览: 464864 次</li>
              <li>性别: <img alt="Icon_minigender_1" src="682834_files/icon_minigender_1.gif" title="男"></li>
              <li>来自: 深圳</li>
              <li><img src="682834_files/offline.gif"></li>
              
            </ul>
          </div>
          <div id="user_visits" class="clearfix">
            <h5>最近访客 <span style="font-weight:normal;font-size:12px;padding-left:30px;"><a href="http://tntxia.iteye.com/blog/user_visits">更多访客&gt;&gt;</a></span></h5>
            
              <div class="user_visit">
                <div class="logo"><a href="http://javachs.iteye.com/" target="_blank"><img alt="javachs的博客" class="logo" src="682834_files/a51e32af-56a1-3a2c-baec-437d1ae3200f-thumb.jpg" title="javachs的博客: javachs"></a></div>
                <div class="left"><a href="http://javachs.iteye.com/" target="_blank" title="javachs">javachs</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://jam-911.iteye.com/" target="_blank"><img alt="jam_911的博客" class="logo" src="682834_files/user-logo-thumb.gif" title="jam_911的博客: "></a></div>
                <div class="left"><a href="http://jam-911.iteye.com/" target="_blank" title="jam_911">jam_911</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://dylinshi126.iteye.com/" target="_blank"><img alt="dylinshi126的博客" class="logo" src="682834_files/user-logo-thumb.gif" title="dylinshi126的博客: "></a></div>
                <div class="left"><a href="http://dylinshi126.iteye.com/" target="_blank" title="dylinshi126">dylinshi126</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://godie.iteye.com/" target="_blank"><img alt="godie的博客" class="logo" src="682834_files/user-logo-thumb.gif" title="godie的博客: "></a></div>
                <div class="left"><a href="http://godie.iteye.com/" target="_blank" title="godie">godie</a></div>
              </div>
            
          </div>

          

                      <div id="blog_menu">
              <h5>文章分类</h5>
              <ul>
                <li><a href="http://tntxia.iteye.com/">全部博客 (510)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17554">Java咖啡厅 (186)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17555">IT杂侃 (22)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17690">程序员笑话 (7)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17725">我的后现代文学 (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17846">WEB编程 (88)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17850">软件评测 (8)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/17867">操作系统 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/18657">炫目Ruby (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/25532">行业知识 (7)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/29027">我的动漫情结 (6)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/18837">其他 (8)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/29371">Window DIY (6)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/29418">古文学 (3)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/29420">转载文章 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/29550">IT百科 (6)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/31206">动态网页Active (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/31207">文档 (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/31208">设计理论 (10)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/31303">.NET 快速开发 (18)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/31312">英语学习 (5)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/33686">网站分析 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/40837">开源软件 (4)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/43261">数据库 (25)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/46444">笔记 (4)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/51008">源代码共享 (8)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/71183">IT人物 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/83573">软件管理 (10)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/86251">小说创作 (0)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/90830">大众软件 (12)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/90831">网站评论 (3)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/96081">小虾乱说 (10)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/98530">游戏 (8)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/113393">心情 (3)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/119316">娱乐休闲 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/141604">Delphi编程 (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/145650">PHP编程 (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/190373">中间件 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/192180">移动开发 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/193038">Office应用 (2)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/268726">Photoshop (1)</a></li>
                
                  <li><a href="http://tntxia.iteye.com/category/268778">Eclipse (5)</a></li>
                
              </ul>
            </div>
            <div id="month_blogs">
              <h5>社区版块</h5>
              <ul>
                <li><a href="http://tntxia.iteye.com/blog/news">我的资讯</a> (0)</li>
                <li>
                  <a href="http://tntxia.iteye.com/blog/post">我的论坛</a> (32)
                </li>
                <li><a href="http://tntxia.iteye.com/blog/answered_problems">我的问答</a> (6)</li>
              </ul>
            </div>
            <div id="month_blogs">
              <h5>存档分类</h5>
              <ul>
                
                  <li><a href="http://tntxia.iteye.com/blog/monthblog/2013-06">2013-06</a> (1)</li>
                
                  <li><a href="http://tntxia.iteye.com/blog/monthblog/2013-04">2013-04</a> (1)</li>
                
                  <li><a href="http://tntxia.iteye.com/blog/monthblog/2013-03">2013-03</a> (9)</li>
                
                <li><a href="http://tntxia.iteye.com/blog/monthblog_more">更多存档...</a></li>
              </ul>
            </div>
            
            
              <div id="comments_top">
                <h5>评论排行榜</h5>
                <ul>
                  
                    <li><a href="http://tntxia.iteye.com/blog/1820617" title="JSON与JAVA的数据转换">JSON与JAVA的数据转换</a></li>
                  
                    <li><a href="http://tntxia.iteye.com/blog/1734870" title="我开始思考未来">我开始思考未来</a></li>
                  
                    <li><a href="http://tntxia.iteye.com/blog/1636945" title="在华为做外包的日子">在华为做外包的日子</a></li>
                  
                    <li><a href="http://tntxia.iteye.com/blog/1820255" title="用Photoshop制作有浮雕效果的文字">用Photoshop制作有浮雕效果的文字</a></li>
                  
                </ul>
              </div>
            

            <div id="guest_books">
              <h5>最新评论</h5>
              <ul>
                
                <li>
                  <a href="http://tntxia.iteye.com/" target="_blank" title="tntxia">tntxia</a>： 
                  jeasonjack 写道你其中好像有错的吧 ！！
哪道题<br>
                  <a href="http://tntxia.iteye.com/blog/1179918#bc2305194">Java面试题（选择题）</a>
                </li>
                
                <li>
                  <a href="http://1454110520.iteye.com/" target="_blank" title="落花时节又逢君">落花时节又逢君</a>： 
                  真厉害，我会向你学习的<br>
                  <a href="http://tntxia.iteye.com/blog/1734870#bc2305180">我开始思考未来</a>
                </li>
                
                <li>
                  <a href="http://jeasonjack.iteye.com/" target="_blank" title="jeasonjack">jeasonjack</a>： 
                  你其中好像有错的吧 ！！<br>
                  <a href="http://tntxia.iteye.com/blog/1179918#bc2305077">Java面试题（选择题）</a>
                </li>
                
                <li>
                  <a href="http://ahack.iteye.com/" target="_blank" title="ahack">ahack</a>： 
                  tntxia 写道json-lib 不好的地方确实是依赖于很多 ...<br>
                  <a href="http://tntxia.iteye.com/blog/1820617#bc2302492">JSON与JAVA的数据转换</a>
                </li>
                
                <li>
                  <a href="http://tntxia.iteye.com/" target="_blank" title="tntxia">tntxia</a>： 
                  json-lib 不好的地方确实是依赖于很多第三方包，但在js ...<br>
                  <a href="http://tntxia.iteye.com/blog/1820617#bc2302409">JSON与JAVA的数据转换</a>
                </li>
                
              </ul>
            </div>

            <div class="local_bottom"></div>
          
        </div>
      </div>

      <div id="footer" class="clearfix">
        <div id="copyright">
          <hr>
          声明：ITeye文章版权属于作者，受法律保护。没有作者书面许可不得转载。若作者同意转载，必须以超链接形式标明文章原始出处和作者。<br>
          © 2003-2012 ITeye.com.   All rights reserved.  [ 京ICP证110151号  京公网安备110105010620 ]
        </div>
      </div>
    </div>
    <script type="text/javascript">
  document.write("<img src='http://stat.iteye.com/?url="+ encodeURIComponent(document.location.href) + "&referrer=" + encodeURIComponent(document.referrer) + "&user_id=' width='0' height='0' />");
</script><img src="682834_files/a.gif" height="0" width="0">

    
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-11104410-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
      
    
  

</body></html>